package com.uzai.console.web.module;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.uzai.common.enums.DeviceAuthTypeEnum;
import com.uzai.common.exception.BusinessException;
import com.uzai.common.result.ResponseCode;
import com.uzai.console.annotation.SystemLogType;
import com.uzai.console.dto.module.merchant.MerchantSetDeviceTypeDto;
import com.uzai.console.dto.module.product.MerchantSetProductDto;
import com.uzai.console.dto.module.product.ProductBuyAuthDto;
import com.uzai.console.dto.module.product.ProductPriceDto;
import com.uzai.console.entity.ProductPrice;
import com.uzai.console.enums.OperationTypeEnum;
import com.uzai.console.enums.SystemModelEnum;
import com.uzai.console.mapper.ProductPriceMapper;
import com.uzai.console.service.module.MerBaseConfigService;
import com.uzai.console.service.module.ProductAuthService;
import com.uzai.console.vo.module.product.BuyProductAuthVo;
import com.uzai.console.vo.module.product.ProductAuthVo;
import com.uzai.console.vo.module.product.ProductPriceVo;
import com.uzai.console.web.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * 产品
 * @author liuqi
 * @date 2021年08月16日 15:11
 */
@Api(value = "功能模块",tags = "模块设置|功能模块")
@ApiSort(3)
@RestController
public class ProductController extends BaseController {
    private Logger logger = LoggerFactory.getLogger(ProductController.class);

    @Autowired
    private ProductAuthService productAuthService;
    @Autowired
    private ProductPriceMapper productPriceMapper;
    @Autowired
    private MerBaseConfigService merBaseConfigService;

    @ApiOperation(value = "查询运营商功能模块列表", notes = "查询运营商功能模块列表", response = ProductAuthVo.class)
    @ApiOperationSupport(order = 1)
    @PostMapping("/product/findProAuthList")
    public Object findProAuthList(){
        return productAuthService.findProAuthList(getLoginInfo().getMerId());
    }

    @ApiOperation(value = "产品价格查询", notes = "产品价格查询", response = ProductPriceVo.class)
    @ApiOperationSupport(order = 2)
    @PostMapping("/product/productPrice")
    public Object devicePrice(@RequestBody ProductPriceDto productPriceDto){

        String productCode = productPriceDto.getProductCode();
        if(StringUtils.isBlank(productCode)){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入产品编码");
        }

        ProductPriceVo productPriceVo = new ProductPriceVo();
        //产品按月购买
        ProductPrice productPrice_month = productPriceMapper.selectByProductCodeAndAuthType(productCode, DeviceAuthTypeEnum.DAT_MONTH.getId());
        if(productPrice_month != null){
            productPriceVo.setMonthPrice(productPrice_month.getPrice());
        }
        //产品按年购买价格
        ProductPrice productPrice_year = productPriceMapper.selectByProductCodeAndAuthType(productCode, DeviceAuthTypeEnum.DAT_YEAR.getId());
        if(productPrice_year != null){
            productPriceVo.setYearPrice(productPrice_year.getPrice());
        }
        return productPriceVo;
    }

    @SystemLogType(model = SystemModelEnum.Product, type = OperationTypeEnum.BUY)
    @ApiOperation(value = "产品购买", notes = "产品购买", response =  BuyProductAuthVo.class)
    @ApiOperationSupport(order = 3)
    @PostMapping("/product/buyProductAuth")
    public Object buyProductAuth(@RequestBody ProductBuyAuthDto productBuyAuthDto){
        return productAuthService.buyProductAuth(productBuyAuthDto);
    }

    @SystemLogType(model = SystemModelEnum.Product, type = OperationTypeEnum.UPDATE)
    @ApiOperation(value = "运营商启用关闭模块", notes = "运营商启用关闭模块", response = Boolean.class)
    @ApiOperationSupport(order = 4)
    @PostMapping("/product/setpRroduct")
    public Object setpRroduct(@RequestBody MerchantSetProductDto merchantSetProductDto){
        //私域模块也关闭
        MerchantSetDeviceTypeDto merchantSetDeviceTypeDto = new MerchantSetDeviceTypeDto();
        BeanUtils.copyProperties(merchantSetProductDto, merchantSetDeviceTypeDto);
        merchantSetDeviceTypeDto.setMerId(merchantSetProductDto.getMerId());
        merchantSetDeviceTypeDto.setKey(merchantSetProductDto.getKey());
        merchantSetDeviceTypeDto.setStatus(merchantSetProductDto.getStatus());
        merBaseConfigService.setDeviceType(merchantSetDeviceTypeDto);

        return productAuthService.setProduct(merchantSetProductDto);
    }

}
